package com.jasonjan.jiance.module.home.content

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.v4.app.ActivityCompat
import android.view.MenuItem
import android.webkit.WebSettings
import android.webkit.WebView
import com.jasonjan.jiance.R
import com.jasonjan.jiance.bean.HomeNews
import com.jasonjan.jiance.global.InitApp
import com.jasonjan.jiance.module.base.BaseActivity
import com.jasonjan.jiance.util.T
import com.r0adkll.slidr.model.SlidrInterface

class HomeWebView : BaseActivity() {


    private var bean:HomeNews.ResultBean.DataBean?=null
    private var webView: WebView?=null
    private var fab:FloatingActionButton?=null

    companion object {
        private val TAG = "HomeWebView"
        fun launch(bean: HomeNews.ResultBean.DataBean) {
            InitApp.AppContext.startActivity(Intent(InitApp.AppContext, HomeWebView::class.java)
                    .putExtra(TAG, bean)
                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_home_web_view)
        initView()
        initData()

        if (Build.VERSION.SDK_INT >= 23) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE), 0)
            }
        }
    }

    fun initView(){
        bean=intent.getParcelableExtra<HomeNews.ResultBean.DataBean>(TAG)
        setTitle(bean!!.title)
        webView=findViewById<WebView>(R.id.home_webview)
        fab=findViewById<FloatingActionButton>(R.id.fab)
        val webSettings: WebSettings =webView!!.settings
        webSettings.javaScriptEnabled=true
        webSettings.blockNetworkImage=false
        webSettings.useWideViewPort=true
        webSettings.loadWithOverviewMode=true
        webSettings.builtInZoomControls=true
        webSettings.setSupportZoom(true)
        webSettings.displayZoomControls=false
        webSettings.setAppCacheEnabled(true)
        webSettings.databaseEnabled=true
        webSettings.domStorageEnabled=true


        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.LOLLIPOP)
            webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE)

        //设置返回按钮
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.getItemId()) {
            android.R.id.home -> finish()
        }
        return super.onOptionsItemSelected(item)
    }

    fun initData(){
        webView!!.loadUrl(bean!!.link+"?id="+bean!!.id)
        fab!!.setOnClickListener{
            T.showShort("点击了浮动按钮！")
        }
    }

    fun getSlidrInterface(): SlidrInterface {
        return slidrInterface
    }

}
